<?php
namespace app\api\controller;

use think\Request;
use app\base\model\Cart as CartModel;
use app\base\model\Collection;
use think\Cache;

class Cart extends Base{
   //购物车模型
    protected $cart;
    //收藏模型
    protected $collect;
    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        $this->cart=new CartModel();
        $this->collect=new Collection();
    }
    /**
     *加入购物车
     * return json
     */
    public function addCart(){
        if(empty($this->userInfo)){
             $msg['message']=checkMsg(LOGIN_ERROR);
            return $msg;
        }
       $goods_id = $this->request->post('goods_id');
       $goods_num= $this->request->post('goods_num',1);
       $com_id   = $this->request->post('com_id',0);
       $goods_price=$this->request->post('goods_price');
       if(empty($goods_id) || empty($goods_price)){
           return $msg['message']=checkMsg(CANSHU_ERROR);
       }
       $cart_data=[
           'goods_id' =>$goods_id,
           'user_id'  =>$this->user_id,
           'com_id'   =>$com_id,
           'goods_num'=>$goods_num,
           'goods_price'=>$goods_price,
           'goods_total_price'=>($goods_price*$goods_num),
           'add_time'   =>time()
       ];
       $res=$this->cart->insertGetId($cart_data);
       if($res){
           return $msg['message']=checkMsg(ADD_SUCCESS);
       }else{
           return $msg['message']=checkMsg(ADD_FAIL);
       }
     return $msg;
    }
    /**
     *修改购物车的数量
     * return json
     */
    public function eidteCartNum(){
        if(empty($this->userInfo)){
            return $msg['message']=checkMsg(LOGIN_ERROR);
        }
        $cart_id = $this->request->post('cart_id');
        $type    = $this->request->post('type','1');  //1:加 0:减
         if(empty($cart_id)){
             return $msg['message']=checkMsg(CANSHU_ERROR);
         }
         $cartInfo=$this->cart->where(['cart_id'=>$cart_id,'user_id'=>$this->user_id])->find();
         if(empty($cartInfo)){
             return $msg['message']=checkMsg(CART_NOT_EXIT);
         }
         if($type){  //增加数量
             $goods_num =$cartInfo['goods_num']+1;
         }else{ //减少数量
             if($cartInfo['goods_num']==1){
                 return $msg['message']=checkMsg(CANNOT_LESS_ONE);
             }
             $goods_num =$cartInfo['goods_num']-1;
         }
        $goods_total_price =$goods_num*$cartInfo['goods_price'];
        $save_data=[
            'goods_num' =>$goods_num,
            'goods_total_price'=>$goods_total_price,
            'update_time' =>time()
        ];
         $save_res=$this->cart->save($save_data,['cart_id'=>$cart_id,'user_id'=>$this->user_id]);
         if($save_res){
             $msg['message']=checkMsg(UPDATE_DATA_SUCCESS);
             $msg['goods_total_price']=$goods_total_price;
         }else{
             $msg['message']=checkMsg(UPDATE_DATA_FAIL);
         }
         return $msg;
    }
   /**
    *选中购物车
    * return json
    */
   public function selectedCart(){
       if(empty($this->userInfo)){
           return $msg['message']=checkMsg(LOGIN_ERROR);
       }
       $cart_id = $this->request->post('cart_id');
       $is_all  = $this->request->post('is_all',0); //0:不是全选 1:是全选
       $cache_name="cart_cache_".$this->user_id;
       if($is_all){
           $cart_list=$this->cart->where(['user_id'=>$this->user_id])->select();
           $cartIds=[];
           foreach($cart_list as $key=>$value){
              $cartIds[]=$value['cart_id'] ;
           }
           if(Cache::has($cache_name)){
               $cart_ids=Cache::get($cache_name);
               if(count($cart_ids)==count($cartIds)){
                   Cache::set($cache_name,[]);
               }else{
                   Cache::set($cache_name,$cartIds);
               }
           }else{
               Cache::set($cache_name,$cartIds);
           }
       }else{
           if(empty($cart_id)){
               return $msg['message']=checkMsg(CANSHU_ERROR);
           }
           $count=$this->cart->where(['cart_id'=>$cart_id,'user_id'=>$this->user_id])->count();
           if($count<1){
               return $msg['message']=checkMsg(CART_NOT_EXIT);
           }
           if(Cache::has($cache_name)){
               $cart_ids=Cache::get($cache_name);
               if(in_array($cart_id,$cart_ids)){
                   $key=array_search($cart_id,$cart_ids);
                   unset($cart_ids[$key]);
               }else{
                   $cart_ids[]=$cart_id;
               }
               Cache::set($cache_name,$cart_ids);
           }else{
               $cart_ids[]=$cart_id;
               Cache::set($cache_name,$cart_ids);
           }
       }
       $cart_ids=Cache::get($cache_name);
       $cart_num=count($cart_ids);
       $total_amount=0.00;
       $cart_list=$this->cart->where(['user_id'=>$this->user_id,'cart_id'=>['in',$cart_ids]])->select();
       foreach($cart_list as $key=>$value){
           $total_amount+=$value['goods_total_price'];
       }
       $msg['message']=checkMsg(GET_DATA_SUCCESS);
       $msg['goods_num']=$cart_num;
       $msg['total_amount']=$total_amount;
       return $msg;
   }
    /**
     *删除购物车
     * return json
     */
    public function deleteCart(){
        if(empty($this->userInfo)){
            return $msg['message']=checkMsg(LOGIN_ERROR);
        }
        $cache_name="cart_cache_".$this->user_id;
        $cart_id=$this->request->post('cart_id');
        if(empty($cart_id)){
          if(Cache::has($cache_name)){
               $cart_ids=Cache::get($cache_name);
               if(empty($cart_ids)){
                   return $msg['message']=checkMsg(SELECT_NOT_CART);
               }else{
                   $res=$this->cart->where(['user_id'=>$this->user_id,'cart_id'=>['in',$cart_ids]])->delete();
               }
          }else{
              return $msg['message']=checkMsg(SELECT_NOT_CART);
          }
        }else{
            $count=$this->cart->where(['user_id'=>$this->user_id,'cart_id'=>$cart_id])->count();
            if($count<1){
                return $msg['message']=checkMsg(CART_NOT_EXIT);
            }else{
                $res=$this->cart->where(['user_id'=>$this->user_id,'cart_id'=>$cart_id])->delete();
            }
        }
        if($res){
            return $msg['message']=checkMsg(DELETE_DATA_SUCCESS);
        }else{
            return $msg['message']=checkMsg(DELETE_DATA_FAIL);
        }
    }
   /**
    *移入收藏夹
    *return json
    */
  public function moveFavorites(){
      if(empty($this->userInfo)){
          return $msg['message']=checkMsg(LOGIN_ERROR);
      }
      $cache_name="cart_cache_".$this->user_id;
      $cart_id=$this->request->post('cart_id');
      if(empty($cart_id)){
          if(Cache::has($cache_name)){
              $cart_ids=Cache::get($cache_name);
              if(empty($cart_ids)){
                  return $msg['message']=checkMsg(SELECT_NOT_CART);
              }else{
                  $cart_list=$this->cart->where(['user_id'=>$this->user_id,'cart_id'=>['in',$cart_ids]])->select();
                  foreach($cart_list as $key=>$value){
                      $collect_data=[
                          'user_id'  =>$this->user_id,
                          'goods_id' =>$value['goods_id'],
                          'add_time' => time()
                      ];
                      $count=$this->collect->where(['user_id'=>$this->user_id,'goods_id'=>$value['goods_id']])->count();
                     if($count<1){
                         $this->collect->insertGetId($collect_data);
                     }
                  }
                  $this->cart->where(['user_id'=>$this->user_id,'cart_id'=>['in',$cart_ids]])->delete();
                  return $msg['message']=checkMsg(MOVE_SUCCESS);
              }
          }else{
              return $msg['message']=checkMsg(SELECT_NOT_CART);
          }
      }else{
        $cartInfo = $this->cart->where(['user_id'=>$this->user_id,'cart_id'=>$cart_id])->find();
        if(empty($cartInfo)){
            return $msg['message']=checkMsg(CART_NOT_EXIT);
        }else{
            $count=$this->collect->where(['user_id'=>$this->user_id,'goods_id'=>$cartInfo['goods_id']])->count();
            if($count<1){
                $data=[
                    'user_id' =>$this->user_id,
                    'goods_id' =>$cartInfo['goods_id'],
                    'add_time' =>time()
                ];
                $this->collect->insertGetId($data);
                $this->cart->where(['user_id'=>$this->user_id,'cart_id'=>$cart_id])->delete();
            }
        }
        return $msg['message']=checkMsg(MOVE_SUCCESS);
      }
  }

}